package com.lagou.rabbitmq.demo.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component
public class MyMeetingListener {

    private static Map<String, Set<String>> payed = new HashMap<>();
    private static Map<String, Set<String>> expire = new HashMap<>();



    @RabbitListener(queues = "q.pay.delay")
    public void onMessage(Message message, Channel channel) throws IOException {

        String body = new String(message.getBody(), message.getMessageProperties().getContentEncoding());
        String[] split = body.split("-");//user-orderId
        String user = split[0];
        String orderId = split[1];
        if(payed.get(user)==null){
            payed.put(user, new HashSet<>());
        }
        if(expire.get(user)==null){
            expire.put(user, new HashSet<>());
        }
        if (!payed.get(user).contains(orderId)) {
            expire.get(user).add(orderId);
        }
        // 消息确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }

    public static Map<String, Set<String>> getPayed() {
        return payed;
    }

    public static void setPayed(Map<String, Set<String>> payed) {
        MyMeetingListener.payed = payed;
    }

    public static Map<String, Set<String>> getExpire() {
        return expire;
    }

    public static void setExpire(Map<String, Set<String>> expire) {
        MyMeetingListener.expire = expire;
    }
}
